$(document).ready(function(){
	$("#tarriff_alert").alert('close');
	$('#add_tariff_from').validate({
	    rules: {
	      start: {
	        required: true
	      },
	      end: {
	        required: true
	      }
	    },
		highlight: function(element) {
			$(element).closest('.control-group').removeClass('success').addClass('error');
		},
		success: function(element) {
			element.text('').addClass('valid').closest('.control-group').removeClass('error').addClass('success');
		},
		submitHandler: function(form) {
			var request = new FormData();                   
			$.each(context.prototype.fileData, function(i, obj) { request.append(i, obj.value.files[0]); });    
			request.append('action', 'upload');
			request.append('id', response.obj.id);
			$.ajax({
			    type        : 'POST',
			    url     	: server+"admin/tariff/upload",
			    data        : request,
			    processData : false,
			    contentType : "multipart/form-data",    
			    success: function(data){
			    	if(data.status=="success"){
			    		$('#tarriff_alert').hide(500);
			    		$('#start').val("");
			    		$('#end').val("");
			    		$('#operatName').html("添加收费表");
			    		$('#saveOrUpdate').html("添加");
			    		$('#tariffModal').modal('hide');
			    		window.location.href=server+"admin/customer/customer_list";
			    	}else{
			    		$('#errormsg').html(data.msg);
			    		$('#tarriff_alert').show(500);
			    	}
			    },
			    error		: function(data){
			    	$('#tarriff_msg').html("添加失败!请重新登录");
		    		$('#tarriff_alert').show(500);
			    },
			    dataType: "JSON"
			});
		}
    });
});

/**
 * 跳转更新页面
 * @param id
 */
function toUpdate(id)
{
	$('#operatName').html("修改收费表");
	$('#saveOrUpdate').html("修改");
	$.ajax({
	    type: 'get',
	    url: server+"admin/tariff/get/"+id,
	    success: function(data){
	    	if(data.status=="success"){
	    		$('#tariffModal').hide(500);
	    		$('#start').val(data.start);
	    		$('#end').val(data.end);
	    		$('#vs').val(data.vs);
	    		$('#image').val(data.img);
	    		$('#tid').val(data.id);
	    		$('#tariffModal').modal('show');
	    	}
	    } ,
	    error:	function(data){
	    	$('#errormsg').html("加载数据失败!请重新登录");
    		$('.alert1').show(500);
	    },
	    dataType: "JSON"
	});
}

/**
 * 关闭弹出层
 */
function closeMyModal()
{
	$('#tariffModal').hide(500);
	$('#start').val("");
	$('#end').val("");
	$('#operatName').html("添加收费表");
	$('#saveOrUpdate').html("添加");
	$('#tariffModal').modal('hide');
}

function add(){
	$('#tariffModal').modal('show');
}

function del(id){
	bootbox.dialog("确定删除吗?", [{
	    "label" : "取消"
	},{
	    "label" : "确定",
	    "class" : "btn-success",
	    "callback": function() {
	    	$.ajax({
	    	    type: 'GET',
	    	    url: server+"admin/tariff/del/"+id ,
	    	    success: function(data){
	    	    	if(data.status=="success"){
	    	    		window.location.href=server+"admin/tariff/list";
	    	    	}else{
	    	    		bootbox.alert(data.msg);
	    	    	}
	    	    } ,
	    	    error:	function(data){
	    	    	bootbox.alert("删除失败");
	    	    },
	    	    dataType: "JSON"
	    	});
	    }
	} 
	]);
}